/**
 * Created by wenguangjia on 2017/4/17.
 */
(function () {
    'use strict';

    angular
        .module('logcenterApp')
        .factory('searchService', searchService);
    searchService.$inject = ['$http', '$q','ACTIVE', 'PropertiesService'];
    function searchService($http, $q,active, propertiesService) {


        return{
            //elasticsearch query
            query:function(data,area,type){
                var deferred = $q.defer();
                if(!type){
                    type = '*';
                }
                if(area == null || area == ""){
                    area = '*-'+active;
                }

                if(type == 'service'){
                    var url='logstash-'+area+'-'+type+'-*,logstash-'+area+'-tele-*,logstash-'+area+'-batch-*';
                }else{
                    var url='logstash-'+area+'-'+type+'-*';
                }

                propertiesService.getProperties().then(function (res) {
                    $http.post(res.esAddress+":9200/"+url+"/_search?pretty",data)
                        .then(function (response) {
                                deferred.resolve(response);
                            },
                            function (reason) {
                                deferred.reject(reason);
                            });


                });
                return deferred.promise;

            },
            simpleQuery:function(data,indexPattern){
                var deferred = $q.defer();

                propertiesService.getProperties().then(function (res) {

                    console.log(res.esAddress + ":9200/" + indexPattern + "/_search?pretty");
                    $http.post(res.esAddress+":9200/"+indexPattern+"/_search?pretty",data)
                        .then(function (response) {
                                deferred.resolve(response);
                            },
                            function (reason) {
                                deferred.reject(reason);
                            });

                });
                return deferred.promise;

            },
            //partition CURD
            getPart:function(){
                var deferred = $q.defer();

                $http.get("api/deploy/getPartitions")
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },
            //partition CURD
            getPartition:function(data){
                var deferred = $q.defer();

                $http.post("api/partitions/search",data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },
            savePartition:function(data){
                var deferred = $q.defer();

                $http.post("api/partitions/save",data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },
            deletePartition:function(data){
                var deferred = $q.defer();

                $http.post("api/partitions/delete",data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },

            //partition`s ip and host CURD
            getPartitionIp2Host:function(data){
                var deferred = $q.defer();
                $http.post("api/partitionip2host/search",data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },
            savePartitionIp2Host:function(data){
                var deferred = $q.defer();

                $http.post("api/partitionip2host/save",data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },
            deletePartitionIp2Host:function(data){
                var deferred = $q.defer();

                $http.delete("api/partitionip2host/delete/"+data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },

            //query deploy activiti
            queryDeploy:function(data){
                var deferred = $q.defer();
                var promise = deferred.promise;
                $http.post("api/deploy/query",data).success(function (resp) {
                    console.log(resp);
                    deferred.resolve(resp);
                }).error(function (resp) {
                    deferred.reject(resp);
                });
                return promise;
            },


            //query and save elasticsearch archive
            queryAction:function(location){
                var deferred = $q.defer();
                $http.get("actionconfig/api/query/"+location)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },
            saveAction:function(location,data){
                var deferred = $q.defer();
                $http.post("actionconfig/api/save/"+location,data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },

            //cluster CURD
            queryCluster:function(){
                var deferred = $q.defer();
                $http.get("api/cluster/query")
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },
            saveCluster:function(data){
                var deferred = $q.defer();
                $http.post("api/cluster/save",data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },
            deleteCluster:function(data){
                var deferred = $q.defer();
                $http.delete("api/cluster/delete/"+data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },

            //FilebeatConfig CURD
            queryFilebeatConfig:function(){
                var deferred = $q.defer();
                $http.get("api/filebeatconfig/query")
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },
            saveFilebeatConfig:function(data){
                var deferred = $q.defer();
                $http.post("api/filebeatconfig/save",data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },
            deleteFilebeatConfig:function(data){
                var deferred = $q.defer();
                $http.delete("api/filebeatconfig/delete/"+data.id)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },

            uploadFilebeatConfig:function(data){
                var deferred = $q.defer();
                $http.post("api/filebeatconfig/upload", data)
                    .then(function (response) {
                            deferred.resolve(response);
                        },
                        function (reason) {
                            deferred.reject(reason);
                        });

                return deferred.promise;
            },

            //base64 to jsEiInfo
            getEiInfo:function(data){
                var deferred = $q.defer();
                propertiesService.getProperties().then(function (res) {
                    $http.post(res.eiinfoAddress+"/WebProxy/api/EIInfo/Deserialize",data)
                        .then(function (response) {
                                var cEiInfo = response.data;
                                var jEiInfo = EI.EIInfoCStoJS(cEiInfo);
                                if(jEiInfo.SysInfo.Flag!=0){
                                    deferred.reject(jEiInfo.SysInfo.Msg);
                                }else{
                                    deferred.resolve(jEiInfo);
                                }
                            },
                            function (reason) {
                                deferred.reject(reason);
                            });
                });

                return deferred.promise;
            }
        }
    }

})();
